package org.weasel.wizards;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.weasel.business.Developer;
import org.weasel.models.Model;


public class SettingsProjectPage extends WizardPage {
	private Combo durationOfIterations;
    private Combo durationOfRelease;
    private Combo numberOfReleases;
    private Combo iterationsPerRelease;
    private List developerList;
    private List selectedList;
    private ArrayList<String> selectedDevelopers;
	@SuppressWarnings("unused")
	private ISelection selection;
	private Model model;
	private Text projStartDate;

	public SettingsProjectPage(ISelection selection, Model model) {
		super("wizardPage");
		setTitle("Project Settings");
        setDescription("Define the settings of the new project.");
        this.selection = selection;
        this.model = model;
        selectedDevelopers = new ArrayList<String>();
        setPageComplete(false);
	}
	
	public void createControl(Composite parent) {
		Label label;
		{
			parent.setSize(525, 223);
		}
		Composite container = new Composite(parent, SWT.NULL);
        GridLayout layout = new GridLayout();
        layout.numColumns = 1;
        container.setLayout(layout);
        
        layout = new GridLayout();
        layout.numColumns = 3;
        TabFolder folder = new TabFolder(container, SWT.NULL);
        GridData gd = new GridData(GridData.FILL_BOTH);
        folder.setLayout(layout);
        folder.setLayoutData(gd);
          
        TabItem project = new TabItem(folder, SWT.NULL);
        project.setText("Project");
        TabItem developers = new TabItem(folder, SWT.NULL);
        developers.setText("Developers");
       
        //initialize first tabitem
        Composite group = new Composite(folder, SWT.NULL);
        project.setControl(group);
        group.setLayout(layout);        
        
        gd = new GridData(GridData.BEGINNING);
        gd.horizontalSpan = 2;
        gd.verticalIndent = 9;
        
        label = new Label(group, SWT.NULL);
        label.setText("Number of releases:");

        numberOfReleases = new Combo(group, SWT.NONE);
        initialize(numberOfReleases);
        numberOfReleases.setLayoutData(gd);
        
        label = new Label(group, SWT.NULL);
        label.setText("Iterations per release:");
  
        iterationsPerRelease = new Combo(group, SWT.NONE);
        initialize(iterationsPerRelease);
        iterationsPerRelease.setLayoutData(gd);
        
        createLine(group, layout.numColumns);
        
//        label = new Label(group, SWT.NULL);
//        label.setText("Duration of release (days):");
//        
//        durationOfRelease = new Combo(group, SWT.NONE);
//        initialize(durationOfRelease);
//        durationOfRelease.setLayoutData(gd);
        
        label = new Label(group, SWT.NULL);
        label.setText("Duration of iteration (days):");
        
        durationOfIterations = new Combo(group, SWT.NONE);
        initialize(durationOfIterations);
        durationOfIterations.setLayoutData(gd);
        
        gd = new GridData(GridData.BEGINNING);
        gd.horizontalSpan = 2;
        gd.verticalIndent = 0;
        label = new Label(group, SWT.NULL);
        label.setText("Start-date:");
        projStartDate = new Text(group, SWT.BORDER | SWT.SINGLE);
        projStartDate.setLayoutData(gd);
        projStartDate.setTextLimit(10);
        projStartDate.setText(new SimpleDateFormat("dd.MM.yyyy").format(new Date()));
        projStartDate.addFocusListener(new org.eclipse.swt.events.FocusAdapter() {
			public void focusLost(org.eclipse.swt.events.FocusEvent e) {
				
			}
		});
//        label = new Label(group, SWT.NULL);
//        label.setText("Stop-date:");
//        projEndDate = new Text(group, SWT.BORDER | SWT.SINGLE);
//        projEndDate.setTextLimit(10);
//        projEndDate.setLayoutData(gd);
        
        
        //initialize second tabitem  
        layout = new GridLayout();
        layout.numColumns = 4;
        group = new Composite(folder, SWT.NONE);
        developers.setControl(group);
        group.setLayout(layout); 
        
        gd = new GridData(GridData.FILL_BOTH);
        
        Group developerGroup = new Group(group, SWT.NONE);
        developerGroup.setLayout(layout);
        developerGroup.setLayoutData(gd);
        developerGroup.setText("Select developers:");
        
        developerList = new List(developerGroup, SWT.V_SCROLL | SWT.MULTI);
        developerList.setLayoutData(gd);
        initialize(developerList);
         
        Button button = new Button(group, SWT.NONE);
        button.setImage(new Image(Display.getCurrent(), getClass().getResourceAsStream("/icons/left_arrow_nav.gif")));
        button.addMouseListener(new MouseAdapter(){
        	@Override
			public void mouseDown(MouseEvent e) {
        		removeSelectedDevelopers();
        	}
        });
        
        button = new Button(group, SWT.NONE);
        button.setImage(new Image(Display.getCurrent(), getClass().getResourceAsStream("/icons/right_arrow_nav.gif")));
        button.addMouseListener(new MouseAdapter(){
        	@Override
			public void mouseDown(MouseEvent e) {
        		getSelectedDevelopers();
        	}
        });
        
        developerGroup = new Group(group, SWT.NONE);
        developerGroup.setLayout(layout);
        developerGroup.setLayoutData(gd);
        developerGroup.setText("Selected developers:");
        
        selectedList = new List(developerGroup, SWT.V_SCROLL | SWT.MULTI);
        selectedList.setLayoutData(gd);
         
        setControl(container);
    }

    protected void removeSelectedDevelopers() {
		// TODO Auto-generated method stub
    	for (int i = 0; i < selectedList.getSelectionCount(); i++) {
    		selectedDevelopers.remove(selectedList.getSelection()[i]);
    		developerList.add(selectedList.getSelection()[i]);
    	}
    	selectedList.remove(selectedList.getSelectionIndices());
    	setPageComplete(isComplete());
    	
	}

	protected void getSelectedDevelopers() {
		// TODO Auto-generated method stub
    	for (int i = 0; i < developerList.getSelectionCount(); i++) {
    		selectedDevelopers.add(developerList.getSelection()[i]);
    		selectedList.add(developerList.getSelection()[i]);
    	}
    	developerList.remove(developerList.getSelectionIndices());
    	setPageComplete(isComplete());
	}

	private boolean isComplete() {
		// TODO Auto-generated method stub
		if (selectedList.getItemCount() != 0) {
			return true;
		} else {
			setErrorMessage("Developers must be specified");
			return false;
		}
	}

	private void initialize(List list) {
		//here the list is filled with developers of the company
		ArrayList<Developer> developers = model.getCompany().getDevelopers();
		for (Developer developer : developers) {
			list.add(developer.getName() + " (" + developer.getId() + ")");
		}
	}

	private void initialize(Combo comboBox) {
		// TODO Auto-generated method stub
    	for (int i = 1; i <= 14; i++) {
			comboBox.add(Integer.toString(i));
		}
    	comboBox.select(0);
	}

    private void createLine(Composite parent, int ncol) {
        Label line = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL |
                SWT.BOLD);
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
        gridData.horizontalSpan = ncol;
        gridData.verticalIndent = 9;
        line.setLayoutData(gridData);
    }
    
    public String getDurationOfRelease() {
    	return durationOfRelease.getText();
    }
    
    public String getNumberOfReleases() {
    	return numberOfReleases.getText();
    }
    
    public String getDurationOfIterations() {
    	return durationOfIterations.getText();
    }
    
    public String getIterationsPerRelease() {
    	return iterationsPerRelease.getText();
    }
    
    public String getProjectStartDate(){
    	return projStartDate.getText();
    }
    
    public ArrayList<Developer> getDevelopers() {
    	//IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		//IViewPart viewPart = page.findView(Navigator.ID);
		//Navigator navigator = (Navigator) viewPart;
		ArrayList<Developer> selDevelopers = new ArrayList<Developer>();
    	for (String string : selectedDevelopers) {
    		//this line retrieves the ID of the selected developers
			String developerID = string.substring(string.indexOf("(") + 1, string.length() - 1);
			selDevelopers.add(model.getCompany().getDeveloper(developerID));
		}
    	return selDevelopers;
    }
}
